﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using WXY.Course.Model;

namespace WXY.Course.DAL
{
    public class RecommendDAL
    {
        private static SqlHelper sqlHelper = new SqlHelper(ConfigurationManager.ConnectionStrings["CourseDB"].ConnectionString);

        private const string SQL_GETLIST = "select * from TRecommend";

        private const string SQL_INSERT = @"Insert into TRecommend(Name,Url,Position,Image)
                                            values(@Name,@Url,@Position,@Image)";

        private const string SQL_DELETE = @"Delete from TRecommend
                                            where Code=@Code";

        public static int Insert(RecommendInfo info)
        {
            SqlParameter[] parameters ={
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Url", info.Url),
                new SqlParameter("@Position", (int)info.Position),
                new SqlParameter("@Image", info.Image)
            };

            return sqlHelper.ExecuteNonQuery(SQL_INSERT, CommandType.Text, parameters);
        }

        public static int Delete(int code)
        {
            SqlParameter[] parameters ={
                new SqlParameter("@Code", code)
            };

            return sqlHelper.ExecuteNonQuery(SQL_DELETE, CommandType.Text, parameters);
        }

        public static IList<RecommendInfo> GetList()
        {
            return sqlHelper.ExecuteQuery(SQL_GETLIST, CommandType.Text, (reader) =>
            {
                IList<RecommendInfo> list = new List<RecommendInfo>();
                while (reader.Read())
                {
                    RecommendInfo recommend = new RecommendInfo();
                    recommend.Code = Convert.ToInt32(reader["Code"]);
                    recommend.Name = reader["Name"].ToString();
                    recommend.Url = reader["Url"].ToString();
                    recommend.Image = reader["Image"].ToString();
                    recommend.Position = (RecommendPosition)Convert.ToInt32(reader["Position"]);
                    list.Add(recommend);
                }
                return list;
            }, null);
        }
    }
}
